package com.o2o.cleaning.month.platform.ebusiness_plat.nyb

import java.time.LocalDate

import com.alibaba.fastjson.JSON
import org.apache.spark.sql.SparkSession
import org.elasticsearch.spark._

object Nyb_ES {
  private val time: LocalDate = LocalDate.now()

  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder()
      .appName("Kuaishou_es")
      .master("local[*]")
      .config("es.nodes", "192.168.1.29")
      .config("es.port", "9200")
      .config("es.net.http.auth.user", "elastic")
      .config("es.net.http.auth.pass", "changeme")
      .config("es.batch.write.retry.count", "10")
      .config("es.batch.write.retry.wait", "60")
      .config("cluster.name", "O2OElastic")
      .getOrCreate()
    val sc = spark.sparkContext

    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")

    /** *
      * 需要修改的参数信息
      */
    val year = time.getYear
    val monthArr = Array(9)
        val indexArr = Array("taobao", "tmall", "jd", "suning", "txd", "guomei", "kaola", "yunji", "dangdang", "miya",  "womaiwang", "rongyigou", "palmlife")
//    val indexArr = Array( "sifulan")

    for (month <- monthArr) {
      for (platform <- indexArr) {
        val index = s"${year}_${platform}/${platform}_${year}_${month}"
        println(" index ---->" + index)
        //good路径
        val goodPath = s"s3a://o2o-dataproces-group/zyf/nyb/result/${platform}/2021/9/all/"

        //入库
        val df = spark.read.orc(goodPath)

        //    df.show(false)
        df.where("sellCount > 0 and salesAmount > 0")

          .toJSON.rdd.map(line => {

          //flavors类型不一致
          val lines = JSON.parseObject(line)

          val salesAmount: Double = lines.getDouble("salesAmount")
          val priceText: Double = lines.getDouble("priceText")

          val sales: Double = salesAmount.formatted("%.2f").toDouble
          val price: Double = priceText.formatted("%.2f").toDouble

          lines.put("salesAmount", sales)
          lines.put("priceText", price)

          lines
        })
          .saveToEs(index, Map("es.mapping.id" -> "good_id"))
      }
    }
  }

}
